<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('执行')" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <input type="hidden" id="pwd" th:value="*{pwd}">
    <input type="hidden" id="pwdHome" th:value="*{pwdHome}">
    <div class="row">
        <div class="col-sm-6">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <select id="type" class="form-control m-b" style="width: 160px;" onchange="onchangeSelect(this)">
                        <option value="1">select</option>
                        <option value="0">other</option>
                    </select>
                    <a class="btn btn-primary btn-rounded btn-sm" onclick="submitHandler('0')" ><i class="fa fa-search"></i>&nbsp;查询</a>
                </div>
                <div class="ibox-content">
                    <div class="form-group">
                        <label class="font-noraml"></label>
                        <div>
                            <textarea  id="value" name="content"  maxlength="30000" style="width: 700px;height: 200px;" ></textarea>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-sm-6" style="display: none" id="other">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <select id="type1"  class="form-control m-b" style="width: 160px;" onchange="onchangeSelect(this)">
                        <option value="2">update</option>
                        <option value="3">insert</option>
                        <option value="4">delete</option>
                        <option value="6">other</option>
                    </select>
                    <a class="btn btn-primary btn-rounded btn-sm" onclick="submitHandler('1')" ><i class="fa fa-search"></i>&nbsp;更新</a>
                </div>
                <div class="ibox-content">
                    <div class="form-group">
                        <label class="font-noraml"></label>
                        <div>
                            <textarea   id="value1" name="content"  maxlength="30000" style="width: 700px;height: 200px;" ></textarea>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="col-sm-12 select-table table-striped">
    <table id="bootstrap-table" class="table text-nowrap"></table>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: summernote-js" />
<th:block th:include="include :: use-js" />
<script type="text/javascript">

        $(document).ready(function () {
        $('.summernote').summernote({
            lang: 'zh-CN'
        });
    });
        var edit = function () {
        $("#eg").addClass("no-padding");
        $('.click2edit').summernote({
        lang: 'zh-CN',
        focus: true
    });
    };
        var save = function () {
        $("#eg").removeClass("no-padding");
        var aHTML = $('.click2edit').summernote('code');
        $('.click2edit').summernote('destroy');
    };
    var prefix = ctx + "db/config";
    function submitHandler(obj) {
        var type = '';
        var selectValue = '';
        if (obj == '0'){
            type = $("#type").val();
            selectValue = $("#value").val();
        }
        if (obj == '1'){
            type = $("#type1").val();
            selectValue = $("#value1").val();
        }
        //查询
        if (type == '1' && $.common.isEmpty($("#value").val())){
            $.modal.alertWarning("查询内容不能为空");
            return;
        }
        //修改
        if (type != '1' && $.common.isEmpty($("#value1").val())){
            $.modal.alertWarning("更新内容不能为空");
            return;
        }
        //通用参数
        var data = {"content":selectValue,"pwd":$("#pwd").val(),"pwdHome":$("#pwdHome").val(),"type":type}
        var json = JSON.stringify(data);
        var key = {"key":$.use.encrypt(json)}
        if (obj == '0'){
            query(key);
        }
        if (obj == '1')  {
            update(key);
        }
    }
    
    /**
     * 查询
     * */
    function query(data) {
        //查询类型渲染表格
        $('#bootstrap-table').bootstrapTable('destroy');
        $('#bootstrap-table').bootstrapTable({
            ajax: function (request) {
                $.ajax({
                    type: "post",
                    url: prefix + "/performBody",
                    dataType: "json",
                    data: data,
                    async:true,
                    success: function (result) {
                        if (result.code != '0' ){
                            $.modal.alertWarning(result.msg);
                            $('#bootstrap-table').bootstrapTable("hideLoading");
                            return;
                        }
                        var r = $.use.decrypt(result.dataRows);
                        if ($.common.isEmpty(r)){
                            $.modal.alertWarning(result.msg);
                            $('#bootstrap-table').bootstrapTable("hideLoading");
                            return;
                        }
                        var json = $.parseJSON(r);
                        if ($.common.isEmpty(json[0])){
                            $('#bootstrap-table').bootstrapTable("hideLoading");
                            return;
                        }
                        var dynamicHeader = [];
                        /*dynamicHeader.push({
                            field: "state",
                            check: true
                        });*/
                        for (var i = 0; i<(Object.keys(json[0])).length; i++) {
                            var property = (Object.keys(json[0]))[i];
                            dynamicHeader.push({
                                "title": property.toLocaleLowerCase(),
                                "field": property,
                                align: 'center',
                                height: 600,
                                formatter: function(value, row, index) {
                                    /*if ($.common.isNotEmpty(value) && value.length > 6){
                                        return $.table.tooltip(value,4);
                                    } else {
                                        return value;
                                    }*/
                                    return value;
                                }
                            });
                        }
                        $('#bootstrap-table').bootstrapTable('destroy').bootstrapTable({
                            data: json,
                            toolbar: '#toolbar',
                            cache: false,
                            striped: true,
                            sidePagination: "client",
                            sortOrder: "desc",
                            pageSize: 25,
                            pageNumber: 1,
                            pageList: "[25, 50, 100, All]",
                            showToggle: true,
                            showColumns: true,
                            showExport: true,
                            exportDataType: "basic",
                            pagination: true,
                            strictSearch: true,
                            search: true,
                            columns: dynamicHeader
                        });
                    },
                    error: function () {
                        $.modal.alertWarning('数据错误');
                        location.reload();
                    }
                });
            }
        });
    }


    /**
     * 修改
     * */
    function update(data) {
          //修改
            var flag = false;
            var msg = '';
            $.ajax({
                type: "post",
                url: prefix + "/performBody",
                dataType: "json",
                data: data,
                async:false,
                success: function (result) {
                    if (result.code != '0'){
                        flag = false;
                        msg = result.msg;
                    } else {
                        msg = result.msg;
                        flag = true;
                    }
                }
            })
            if (!flag){
                $.modal.alertWarning(msg);
            } else {
                $.modal.msgSuccess(msg);
            }
        return;
    }


    /**
     * select变动
     * @param obj
     */
    function onchangeSelect(obj) {
        if (obj.value == '0'){
            $("#type").val('1');
            layer.prompt({title: '请先验证口令', formType: 1}, function(pass, index){
                var currentPwd = $.use.encrypt(pass);
                var data = {"pwd1":currentPwd,"two":"0","pwd":$("#pwd").val(),"pwdHome":$("#pwdHome").val(),"one":"0"}
                var json = JSON.stringify(data);
                var key = {"key":$.use.encrypt(json)}
                $.ajaxSettings.async = false;
                var flag = false;
                var msg = '';
                $.post(prefix + "/pwd",key,function (result) {
                    if (result.code != '0'){
                        flag = false;
                        msg = result.msg;
                    } else {
                        msg = result.msg;
                        flag = true;
                    }
                })
                if (!flag){
                    $.modal.alertWarning(msg);
                    return;
                }else {
                    layer.close(index);
                    $("#other").show();
                }
            });
        }
    }


    
    
</script>
</body>
</html>
